package com.pig4cloud.pig.module.controller.product;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.product.OrderAppraise;
import com.pig4cloud.pig.module.service.product.IOrderAppraiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 评价Controller
 *
 * @author ruoyi
 * @date 2021-08-24
 */
@Validated
@Api(value = "评价控制器", tags = {"评价管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/product/appraise")
public class OrderAppraiseController {

    private final IOrderAppraiseService orderAppraiseService;

    /**
     * 查询评价列表
     */
    @ApiOperation("查询评价列表")
    @PreAuthorize("@pms.hasPermission('module:appraise:list')")
    @GetMapping("/list")
    public TableDataInfo<OrderAppraise> list(@Validated OrderAppraise orderAppraise) {
        return PageUtils.buildDataInfo(orderAppraiseService.page(PageUtils.buildPage()));
    }

    /**
     * 获取评价详细信息
     */
    @ApiOperation("获取评价详细信息")
    @PreAuthorize("@pms.hasPermission('module:appraise:query')")
    @GetMapping("/{id}")
    public R<OrderAppraise> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        OrderAppraise orderAppraise = orderAppraiseService.getById(id);
        return R.ok(orderAppraise);
    }

    /**
     * 新增评价
     */
    @ApiOperation("新增评价")
    @PreAuthorize("@pms.hasPermission('module:appraise:add')")
    @SysLog("评价")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody OrderAppraise orderAppraise) {
        return R.okByRow(orderAppraiseService.save(orderAppraise) ? 1 : 0);
    }

    /**
     * 修改评价
     */
    @ApiOperation("修改评价")
    @PreAuthorize("@pms.hasPermission('module:appraise:edit')")
    @SysLog("评价")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody OrderAppraise orderAppraise) {
        return R.okByRow(orderAppraiseService.updateById(orderAppraise) ? 1 : 0);
    }

    /**
     * 删除评价
     */
    @ApiOperation("删除评价")
    @PreAuthorize("@pms.hasPermission('module:appraise:remove')")
    @SysLog("评价")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(orderAppraiseService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
